import type { DocForm, OaWorkflowRole, OaWorkflowStatus, UserTask } from "@/types/workflow";
import * as workflowApi from '@/api/workFlowApi'
import {useUserStore} from '@/stores/index'

const userStore = useUserStore();

export default class PrivillageChecker{
    
    currentStatus:OaWorkflowStatus|null = null
    userTask:UserTask|null = null
    roleList:OaWorkflowRole[]|null = null;
    docFormStatus:OaWorkflowStatus|null = null;
    docForm:DocForm| null = null;

    constructor(currentStatus?:OaWorkflowStatus,task?:UserTask,docFormStatus?:OaWorkflowStatus,docForm?:DocForm){
        if(currentStatus){
            this.currentStatus = currentStatus;
        } 
        
        if(task)this.userTask = task;
        if(docFormStatus)this.docFormStatus = docFormStatus;

        if(docForm){
            this.docForm = docForm;
        }

        
    }

    /**
     * 是否有审核权限
     */
    async hasAuditPrivillage(){
        

        if(!this.currentStatus){
            return false;
        }
    
        if(this.currentStatus.endPoint){//已完结，没有任何权限
            return;
        }

        const userId = userStore.staffId;

        if(this.roleList == null){ //初始化rolelist
            const reuslt = await workflowApi.getRolesByUser(userId) //;
            if(reuslt.data){
                this.roleList = reuslt.data;
            }else{
                this.roleList = [];
            }             
        }


    
        const roleId = this.currentStatus.roleId; //当前状态对应的角色,如果没有角色，则没有审批权限
    
    
          //check privilege, to see if has priv to edit
          let hasPrivilege = false;
          //check receiver
          if (this.userTask != null &&  userId== this.userTask.receiverId){
              hasPrivilege = true;
          }else{
            for(var i=0;i<this.roleList.length;i++){
                const role = this.roleList[i];
                if (roleId == role.roleId || roleId == role.parentId){
                    hasPrivilege = true;
                    break;
                }
            }
              
          }
    
          return hasPrivilege;
    }

    hasDonePrivillageAndFormDone():boolean{

        //说明当前用户未批示，但是此文件已经被管理员办结。因此当前用户无法再批示
        if (!this.currentStatus?.endPoint && this.docFormStatus?.endPoint){
            if (!this.userTask?.readOnly){                
                return true;
            }
        }

        return false;

    }

    hasReadPrivillageAndFormDone():boolean{ 
        //说明当前用户未批示，但是此文件已经被管理员办结。
        if (!this.currentStatus?.endPoint && this.docFormStatus?.endPoint){
            if (this.userTask?.readOnly){
                return true;   
            }
        } 
        return false;
    }

    hasSavePrivillage(){
        if (this.userTask?.sealed){
            return false;
       }

        //set save button
        if (this.currentStatus?.approveInForm || this.currentStatus?.startPoint){
            return true;
        }

        return false;
        
    }

    /**
     * 是否有补充批注的权限
     */
    hasCommentPrivillage(){
          //已完成，只有docform，没有task    
        if (!this.currentStatus?.startPoint && (!this.userTask) &&  this.docForm){
            return true;
        }     

            
        //已完成，只能补充批注
        if (this.userTask?.done){
            return true;
        }

        return false;
    }
            
}